package com.example.administrator.yunniuusedcar.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.example.administrator.yunniuusedcar.R;

// Android实现左右滑动效果
public class ImagerZoomActivity extends Activity implements OnGestureListener {
    private ViewFlipper flipper;
    private GestureDetector detector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imager_zoom);

        detector = new GestureDetector(this);
        flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);

        flipper.addView(addTextView(R.mipmap.bxh));  //测试图片
        flipper.addView(addTextView(R.mipmap.sellcar));
        flipper.addView(addTextView(R.mipmap.yuyun2));
        flipper.addView(addTextView(R.mipmap.paoche));
        flipper.addView(addTextView(R.mipmap.paoche2));
    }

    private View addTextView(int id) {
        ImageView iv = new ImageView(this);
        iv.setImageResource(id);
        return iv;
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {   //
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {   //

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {   //
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {    //

        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {   //

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {   //
        if (e1.getX() - e2.getX() > 120) {
            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
            this.flipper.showNext();
            return true;
        } else if (e1.getX() - e2.getX() < -120) {
            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
            this.flipper.showPrevious();
            return true;
        }

        return false;
    }
}
